<template>
  <div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
        <a-button type="primary" @click="handleMoreDelete"> 删除 </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              tooltip: '删除',
              popConfirm: {
                title: '是否确认删除',
                placement: 'topRight',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <ExpirationReminderDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, h } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import ExpirationReminderDrawer from './ExpirationReminderDrawer.vue';
  import { useModal } from '/@/components/Modal';
  import { columns, searchFormSchema } from './expirationReminder.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRouter } from 'vue-router';
  import { useGo } from '/@/hooks/web/usePage';
  import { getFormListRemind, getFormDelRemind } from '/@/api/formDesign/formDesign';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();

  export default defineComponent({
    name: 'ExpirationReminder',
    components: {
      BasicTable,
      ExpirationReminderDrawer,
      TableAction,
    },
    props: {
      code: {
        type: String as PropType<String>,
        default: '',
      },
    },
    setup(props) {
      const { resolve } = useRouter();
      const { createMessage, createConfirm } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();

      const [registerTable, { reload, getSelectRowKeys, setSelectedRowKeys, setProps }] = useTable({
        title: '提醒',
        api: getFormListRemind,
        columns,
        formConfig: {
          labelWidth: 150,
          schemas: searchFormSchema,
        },
        searchInfo: {
          action: 'search',
        }, //额外的参数
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        clickToRowSelect: false,
        indexColumnProps: { width: 50 },
        showIndexColumn: false,
        beforeFetch: (info) => {
          let newInfo = info;
          if (props.code) {
            newInfo.code = props.code;
          }
          return newInfo;
        },
        rowKey: 'id',
        rowSelection: {
          type: 'checkbox',
        },
        actionColumn: {
          width: 140,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          record: {
            code: props.code,
          },
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      const handleMoreDelete = () => {
        const selectRowKeys = getSelectRowKeys();
        if (selectRowKeys.length == 0) {
          return createMessage.warning('请选择数据');
        }
        const ids = selectRowKeys.join(',');
        deleteAction(ids);
      };

      const handleDelete = async (record: Recordable) => {
        try {
          const params = {
            ids: record.id,
          };
          setProps({ loading: true });
          await getFormDelRemind(params);
          createMessage.success('操作成功');
          handleSuccess();
        } catch (err) {
          console.error(err);
        } finally {
          setProps({ loading: false });
        }
      };

      const deleteAction = (ids) => {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', t('common.prompt')),
          content: () => h('span', '您确定要删除么'),
          onOk: async () => {
            try {
              const params = {
                ids: ids,
              };
              setProps({ loading: true });
              await getFormDelRemind(params);
              createMessage.success('操作成功');
              handleSuccess();
            } catch (err) {
              console.error(err);
            } finally {
              setProps({ loading: false });
            }
          },
        });
      };

      function handleSuccess() {
        setSelectedRowKeys([]);
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleMoreDelete,
        handleDelete,
        handleSuccess,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
      };
    },
  });
</script>
